/*
** string.c for  in /home/lund/svn/myirc-2016-dewulf_f/src
** 
** Made by florian dewulf
** Login   <dewulf_f@epitech.net>
** 
** Started on  Wed Apr 24 13:33:53 2013 florian dewulf
** Last update Sat May  4 11:43:25 2013 kevin lansel
*/

#include	<math.h>
#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	"strace.h"

char		*append(char *str, char *put)
{
  char		*new;

  if (str)
    new = xmalloc((strlen(str) + strlen(put) + 1) * sizeof(char));
  else
    {
      new = strdup(put);
      return (new);
    }
  new[0] = '\0';
  strcat(new, str);
  new[strlen(str)] = '\0';
  strcat(new, put);
  new[strlen(str) + strlen(put)] = '\0';
  free(str);
  return (new);
}

u_int		count_word(char *str)
{
  u_int		i;
  u_int		j;

  i = 0;
  j = 0;
  while (str[i] == ' ' || str[i] == '\t')
    i++;
  if (str[i])
    j++;
  while (str[i])
    {
      if (str[i] == ' ' || str[i] == '\t')
	{
	  j++;
	  while (str[i] == ' ' || str[i] == '\t')
	    i++;
        }
      else
	i++;
    }
  return (j);
}

void		epurspace(char *buffer)
{
  u_int		i;
  u_int		j;

  i = 0;
  while (buffer[i] == ' ' || buffer[i] == '\t')
    i++;
  while (buffer[i])
    {
      if (buffer[i] == ' ' || buffer[i] == '\t')
	{
	  buffer[i] = ' ';
	  i++;
	  j = i - 1;
	  while (buffer[i] == ' ' || buffer[i] == '\t')
	    i++;
	  if (!buffer[i])
	    {
	      buffer[j] = '\0';
	      i--;
	    }
	}
      else
	i++;
    }
  buffer[i] = '\0';
}

char		**strtowordtab(char *str)
{
  char		**tab;
  u_int		nb;

  nb = count_word(str) + 1;
  tab = xmalloc(nb * sizeof(char *));
  nb = 1;
  tab[0] = strtok(str, " ");
  while ((tab[nb] = strtok(str, " ")) == NULL)
    nb++;
  return (tab);
}

int		end_str(char *str)
{
  int		i;

  i = -1;
  while (++i < 8)
    {
      if (str[i] == '\0' || str[i] == '\n')
	return (0);
    }
  return (1);
}
